<?php
	
	namespace Aspic;
	
	/**
	* Contains usefull methods
	* 
	* @Author Mallory Dessaintes <mdessaintes@gmail.com>
	* @version 1.0
	*/
	class Util {
		
		/**
		* Strip slashes in multi-dimensionnals array
		*/
		public static function stripslashesDeep($value) {
			$value = is_array($value) ?
						array_map(array(get_class(),'stripslashesDeep'), $value) :
						stripslashes($value);

			return $value;
		}
		
		/**
		* Return the "Late State Binding" class name of an object (or string) (whitout namespace before)
		*/
		public static function getClassName($object) {
			if (!is_object($object) && !is_string($object)) {
				throw new \Exception('$object must be an object or a string');
			}
		   
			$class = explode('\\', (is_string($object) ? $object : get_class($object)));
			return $class[count($class) - 1];
		}
		
		/**
		* Return the "Late State Binding" namespace name of an object
		*/
		public static function getNamespaceName($object) {
			if (!is_object($object) && !is_string($object)) {
				throw new \Exception('$object must be an object or a string');
			}
		   
			$classPath = (is_string($object) ? $object : get_class($object));
			$pos = strrpos($classPath, '\\');
			
			if($pos === false) {
				$res = $classPath;
			}
			else {
				$res = substr($classPath, 0, $pos);
			}
			
			return $res;
		}
		
		/**
		* Get the value of $var if it is set (php isset)
		* !/!\! If the var is not set when function is called, php set the var to null even if you don't want to set it !
		* 
		* @param string $convertType The return value could be convert to a specific type (string, int, float, bool, see php settype function)
		*/
		public static function getIfIsSet(&$var, $convertType = null) {
			if(isset($var)) {
				if(!is_null($convertType)) {
					$val = $var;
					
					settype($val, $convertType);
					
					return $val;
				}
				else {
					return $var;
				}
			}
			else {
				return null;
			}
		}
		
		public static function getIfSet(&$var) {
			return static::getIfIsSet($var);
		}

		/**
		* Return trueValue or falseValue if var is true / false 
		*/
		public static function ifTrue($var, $trueValue, $falseValue = '') {
			if($var) {
				return $trueValue;
			}
			else {
				return $falseValue;
			}
		}
		
		public function ifFalse($var, $falseValue, $trueValue = '') {
			return static::ifTrue($var, $trueValue, $falseValue);
		}
		
		/**
		* Return trueValue or falseValue if var is true / false 
		*/
		public static function ifSet(&$var, $replacer) {
			$data = static::getIfIsSet($var);
			
			if($data !== null) {
				return $data;
			}
			else {
				return $replacer;
			}
		}
		
		public static function var2String($var) {
			return print_r($var, true);
		}
		
		/**
		* Return true if $v1 == $v2 OR if $v1 is in array $v2
		*/
		public static function egalOrInArray($v1, $v2) {
			return ($v1 == $v2 OR (is_array($v2) AND in_array($v1, $v2)));
		}
		
		/**
		* Check if $toCheck bits that are set 1 are also set to 1 in $have
		*/
		public static function bitsMatch($have, $toCheck) {
			if(substr_count($toCheck, '1') == 1) {
				return (bool)($have & $toCheck);
			}
			else {		
				$arrRip = str_split((string)decbin($have));
				$arrBinTr = str_split((string)decbin($toCheck));
				$count = count($arrRip);
				
				$arrBinTr = array_reverse(array_pad(array_reverse($arrBinTr), $count, 0));
				
				
				for ($i = 0; $i < $count; ++$i) {
					if($arrBinTr[$i] == 1 AND $arrRip[$i] == 0) {
						return false;
					}
				}
				
				return true;
			}
		}
		
		/**
		 * Test if $className belong to one of the $namespaces and return the first in which class exists 
		 * or return $className if it is a full class path. Return null if none of above
		 * @param type $className
		 * @param array $namespaces
		 * @return string|null
		 */
		public static function getExistingClass($className, array $namespaces) {
			if (class_exists($className)) {
				return $className;
			}

			foreach($namespaces as $ns) {
				$classPath = $ns.'\\'.$className;
				
				if (class_exists($classPath)) {
					return $classPath;
				}
			}
			
			return null;

		}
		
	}	
	
?>